package com.kzd.sdk.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.core.KZD_SdkController;
import com.kzd.sdk.entity.KZD_UserInfo;
import com.kzd.sdk.utils.AppUtils;

public class KZD_UserPassActivity extends OwnerActivity {

    public static void open(Context context) {
        Intent intent = new Intent(context, KZD_UserPassActivity.class);
        context.startActivity(intent);
    }

    private TextView tvUserPassTitle,tvUserPassSubmitUp, tvUserPassSubmitSet;
    private EditText etUserPassOld, etUserPassNew, etUserPassSet;

    private KZD_HttpCallback<String> callbackPassword = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            toast(data);
            finish();
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_activity_user_pass";
    }

    @Override
    public View getLayoutView() {
        return null;
    }

    @Override
    public void initView() {

        tvUserPassTitle = findViewById(AppUtils.getId((this), "tvUserPassTitle"));
        etUserPassOld = findViewById(AppUtils.getId((this), "etUserPassOld"));
        etUserPassNew = findViewById(AppUtils.getId((this), "etUserPassNew"));
        etUserPassSet = findViewById(AppUtils.getId((this), "etUserPassSet"));
        tvUserPassSubmitUp = findViewById(AppUtils.getId((this), "tvUserPassSubmitUp"));
        tvUserPassSubmitSet = findViewById(AppUtils.getId((this), "tvUserPassSubmitSet"));

        findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });

        tvUserPassSubmitUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = etUserPassOld.getText().toString();
                String newPassword = etUserPassNew.getText().toString();
                if (TextUtils.isEmpty(oldPassword)) {
                    toast("请输入旧密码");
                } else if (TextUtils.isEmpty(newPassword)) {
                    toast("请输入新密码");
                } else {
                    KZD_ApiManager.httpChangePassword(oldPassword, newPassword, callbackPassword);
                }
            }
        });

        tvUserPassSubmitSet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String password = etUserPassSet.getText().toString();
                if (TextUtils.isEmpty(password)) {
                    toast("请输入密码");
                } else {
                    KZD_ApiManager.httpSetPassword(password, callbackPassword);
                }
            }
        });
    }

    @Override
    public void initData() {
        fillData(KZD_SdkController.getUserInfo());
    }

    private void fillData(KZD_UserInfo userInfo) {
        if (userInfo == null) {
            return;
        }
        if (userInfo.getIs_pwd() == 1) {
            tvUserPassTitle.setText("修改密码");
            tvUserPassSubmitUp.setVisibility(View.VISIBLE);
            etUserPassOld.setVisibility(View.VISIBLE);
            etUserPassNew.setVisibility(View.VISIBLE);
            tvUserPassSubmitSet.setVisibility(View.GONE);
            etUserPassSet.setVisibility(View.GONE);
        } else {
            tvUserPassTitle.setText("设置密码");
            tvUserPassSubmitUp.setVisibility(View.GONE);
            etUserPassOld.setVisibility(View.GONE);
            etUserPassNew.setVisibility(View.GONE);
            tvUserPassSubmitSet.setVisibility(View.VISIBLE);
            etUserPassSet.setVisibility(View.VISIBLE);
        }
    }
}
